import tkinter as tk
from typing import List
from tkinter.simpledialog import *
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from tkinter.filedialog import *
import docx
# pip3 install python-docx

import windows.WindowsMain as WindowsMain
from windows.RadioBtn import RadioGroup, Box
from windows.SubWindows.SubWindow import SubWindow
from windows.SubWindows.SubWindowWidget import WidgetStyleConfig


class Event:
    @staticmethod
    def Entry_clicked(widget, checkValue, isPwd=False):
        """
        Entry点击事件
        @param widget:
        @param checkValue:
        @param isPwd:
        @return:
        """
        if widget.get() == checkValue:
            print("清空")
            if isPwd: widget.config(show="*")
            widget.delete(0, tk.END)

    @staticmethod
    def Entry_leave(w, c, isPwd=False):
        """
        Entry leave事件
        @param w:
        @param c:
        @param isPwd:
        @return:
        """
        print("激活")
        if w.get() == "":
            if isPwd:
                w.config(show="")
            w.insert(0, c)

    @staticmethod
    def get_config(frame):
        """
        获取配置信息
        @return:
        """

        return frame.make_keys_flag.GetActiveId(),frame.seq_flag.GetActiveId(), frame.entry_name.get()

    @staticmethod
    def new_word(frame):
        """
        生成word试卷
        @return:
        """
        key, seq, name = Event.get_config(frame)

        doc = docx.Document()
        path = asksaveasfilename()
        path += ".docx"

        t0 = doc.add_heading(name, 0)
        t0.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

        doc.save(path)
    @staticmethod
    def new_many_word(frame):
        """
        批量生成栓
        @return:
        """
        Event.get_config(frame)
        askinteger(title = "请输入试卷数字",
                     prompt = "生产出来的试卷名称将用数字标记")



def get_config(text, frame) -> List:
    """
    快速配置一个询问控件组 并返回
    flag_make_keys_label, btn, btn2, group_make_keys 和 RadioGroup
    @param text:
    @param frame:
    @return:
    """
    flag_make_keys_label = tk.Label(frame, text=text, bg='#2B2B2B', fg='white', font=('黑体', 20))
    group_make_keys = RadioGroup()
    btn = WindowsMain.Box(
        unActiveBackColor='#2B2B2B',
        unActiveFontColor='white',
        text="是的",
        ActiveBackColor="green",
        ActiveFontColor='white',
        Group=group_make_keys,
        GroupId="yes",
        myWin=frame,
        DefaultActive=True
    )
    btn2 = WindowsMain.Box(
        unActiveFontColor='white',
        text="算了",
        ActiveBackColor="red",
        ActiveFontColor='white',
        Group=group_make_keys,
        unActiveBackColor='#2B2B2B',
        GroupId="no",
        myWin=frame,
        DefaultActive=False
    )
    return flag_make_keys_label, btn, btn2, group_make_keys


class TestMaker(SubWindow):
    def __init__(self, windows: WindowsMain):
        """
        试卷生成页
        @param windows: WindowsMain 类
        """
        windows.state = "试卷生成"
        self.window = windows

        self.titleWSC = WidgetStyleConfig
        self.titleWSC.text = "试卷生成"
        super().__init__(windows)

    def boxPack(self):
        self.SubWindowWidget.title(WidgetStyleConfig=self.titleWSC, position="center")

        # region 左侧栏

        # 标题的设置
        frame = self.SubWindowWidget.pack_sub_left()
        # Frame的获取
        frame['bg'] = '#2B2B2B'

        # region 输出答案卷
        self.make_keys_flag: RadioGroup
        self.seq_flag: RadioGroup
        f, on_out_key, off_out_key, self.make_keys_flag = get_config("输出答案卷", frame)
        f.pack()
        labelOn, labelOff = on_out_key.getLabel(), off_out_key.getLabel()
        labelOn.pack()
        labelOff.pack()
        # endregion

        tk.Label(frame, bg='#2B2B2B').pack()
        # region 答卷题卷分开
        f2, on_seq, off_seq, self.seq_flag = get_config("答卷题卷分开", frame)
        f2.pack()
        labelOn, labelOff = on_seq.getLabel(), off_seq.getLabel()
        labelOn.pack()
        labelOff.pack()
        # endregion

        tk.Label(frame, bg='#2B2B2B').pack()
        tk.Label(frame, bg='#2B2B2B').pack()
        tk.Label(frame, bg='#2B2B2B').pack()

        self.entry_name = tk.Entry(frame, font=('黑体', 20))
        self.entry_name.insert(0, "试卷标题")
        self.entry_name.bind("<FocusIn>", lambda e: Event.Entry_clicked(self.entry_name, "试卷标题"))
        self.entry_name.bind("<FocusOut>", lambda e: Event.Entry_leave(self.entry_name, "试卷标题"))
        self.entry_name.pack()
        # endregion

        del frame
        # region 右侧栏

        rframe = self.SubWindowWidget.pack_sub_right()
        rframe['bg'] = '#2B2B2B'

        flable_mk_type = tk.LabelFrame(rframe, text='生成试卷类型', bg='#2B2B2B', fg='white')
        flable_mk_type.place(relx=0, rely=0, relwidth=.8, relheight=.34)

        flable_opare = tk.LabelFrame(rframe, text='操作台', bg='#2B2B2B', fg='white')
        flable_opare.place(relx=0, rely=.36, relwidth=.8, relheight=.24)

        group_gen = RadioGroup()
        A = Box('red', 'white', "开启倒计时", "green", "white", group_gen, "Okay", flable_mk_type, "xx", DefaultActive=False)
        B = Box('red', 'white', "关闭倒计时", "green", "white", group_gen, "Over", flable_mk_type, "no", DefaultActive=False)


        word_btn = tk.Button(flable_opare, bg='#FFCD22', text='生成!', command=lambda: Event.new_word(self))
        word_btn['font'] = ("华文中宋", 14)
        word_btn.place(relx=.14, rely=.25, relwidth=.22, relheight=.4)

        word_btn2 = tk.Button(flable_opare, bg='#FFCD22', text='批量生成', command=lambda: Event.new_many_word(self))
        word_btn2['font'] = ("华文中宋", 14)
        word_btn2.place(relx=.14 + .22 + .24, rely=.25, relwidth=.22, relheight=.4)

        # endregion


if __name__ == '__main__':
    ...
